package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateRoomAccountRecord;
import com.ruoyi.system.service.module.estate.IEstateRoomAccountRecordService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 小区房屋套内账户充值/消费记录Controller
 *
 * @author ruoyi
 * @date 2024-08-04
 */
@Validated
@Api(value = "小区房屋套内账户充值/消费记录控制器", tags = {"小区房屋套内账户充值/消费记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/accountRecord")
public class EstateRoomAccountRecordController extends BaseController {

    private final IEstateRoomAccountRecordService estateRoomAccountRecordService;
	private final IPropertyService propertyService;
	private final IEstateService estateService;
	private final ISysUserService sysUserService;

    /**
     * 查询小区房屋套内账户充值/消费记录列表
     */
    @ApiOperation("查询小区房屋套内账户充值/消费记录列表")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateRoomAccountRecord> list(@Validated EstateRoomAccountRecord estateRoomAccountRecord) {
		SysUser sysUser = getUser();
        QueryWrapper<EstateRoomAccountRecord> qw = new QueryWrapper<>();
		if (sysUser.getUserType()==2){
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}else {
				qw.eq("property_id",sysUser.getPropertyId());
				if (estateRoomAccountRecord.getEstateId()!=null){
					qw.eq("estate_id",estateRoomAccountRecord.getEstateId());
				}
			}
		}else if (sysUser.getUserType()==3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if (estateRoomAccountRecord.getPropertyId()!=null){
				qw.eq("property_id",estateRoomAccountRecord.getPropertyId());
			}
			if (estateRoomAccountRecord.getEstateId()!=null){
				qw.eq("estate_id",estateRoomAccountRecord.getEstateId());
			}
		}
		if (estateRoomAccountRecord.getRoomNo()!=null){
			qw.like("room_no",estateRoomAccountRecord.getRoomNo());
		}
		if (estateRoomAccountRecord.getRechargeWay()!=null){
			qw.eq("recharge_way",estateRoomAccountRecord.getRechargeWay());
		}
		if (estateRoomAccountRecord.getRechargeOrUse()!=null){
			qw.eq("recharge_or_use",estateRoomAccountRecord.getRechargeOrUse());
		}
		if (estateRoomAccountRecord.getRoomId()!=null){
			qw.eq("room_id",estateRoomAccountRecord.getRoomId());
		}
		if(StrUtil.isNotEmpty(estateRoomAccountRecord.getBeginDate())){
			qw.between("create_time",estateRoomAccountRecord.getBeginDate() + " 00:00:00",estateRoomAccountRecord.getEndDate() + " 23:59:59");
		}
		qw.orderByDesc("create_time");
        TableDataInfo<EstateRoomAccountRecord> td = PageUtils.buildDataInfo(estateRoomAccountRecordService.page(PageUtils.buildPage(),qw));
        List<EstateRoomAccountRecord> list = td.getRows();
		if (list.size()>0){
			for (EstateRoomAccountRecord record:list){
				if (record.getPropertyId()!=null){
					record.setPropertyName(propertyService.findById(record.getPropertyId()).getPropertyName());
				}
				if (record.getEstateId()!=null){
					record.setEstateName(estateService.findById(record.getEstateId()).getEstateName());
				}
				if (record.getSysUserId()!=null){
					record.setSysUserName(sysUserService.getById(record.getSysUserId()).getNickName());
				}
			}
		}
        return td;
    }

    /**
     * 导出小区房屋套内账户充值/消费记录列表
     */
    @ApiOperation("导出小区房屋套内账户充值/消费记录列表")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:export')")
    @Log(title = "小区房屋套内账户充值/消费记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateRoomAccountRecord> export(@Validated EstateRoomAccountRecord estateRoomAccountRecord) {
		SysUser sysUser = getUser();
		QueryWrapper<EstateRoomAccountRecord> qw = new QueryWrapper<>();
		if (sysUser.getUserType()==2){
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}else {
				qw.eq("property_id",sysUser.getPropertyId());
				if (estateRoomAccountRecord.getEstateId()!=null){
					qw.eq("estate_id",estateRoomAccountRecord.getEstateId());
				}
			}
		}else if (sysUser.getUserType()==3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if (estateRoomAccountRecord.getPropertyId()!=null){
				qw.eq("property_id",estateRoomAccountRecord.getPropertyId());
			}
			if (estateRoomAccountRecord.getEstateId()!=null){
				qw.eq("estate_id",estateRoomAccountRecord.getEstateId());
			}
		}
		if (estateRoomAccountRecord.getRoomNo()!=null){
			qw.like("room_no",estateRoomAccountRecord.getRoomNo());
		}
		if (estateRoomAccountRecord.getRechargeWay()!=null){
			qw.eq("recharge_way",estateRoomAccountRecord.getRechargeWay());
		}
		if (estateRoomAccountRecord.getRechargeOrUse()!=null){
			qw.eq("recharge_or_use",estateRoomAccountRecord.getRechargeOrUse());
		}
		if (estateRoomAccountRecord.getRoomId()!=null){
			qw.eq("room_id",estateRoomAccountRecord.getRoomId());
		}
		if(StrUtil.isNotEmpty(estateRoomAccountRecord.getBeginDate())){
			qw.between("create_time",estateRoomAccountRecord.getBeginDate() + " 00:00:00",estateRoomAccountRecord.getEndDate() + " 23:59:59");
		}
		qw.orderByDesc("create_time");
        List<EstateRoomAccountRecord> list = estateRoomAccountRecordService.list(qw);
        ExcelUtil<EstateRoomAccountRecord> util = new ExcelUtil<EstateRoomAccountRecord>(EstateRoomAccountRecord.class);
        return util.exportExcel(list, "小区房屋套内账户充值/消费记录");
    }

    /**
     * 获取小区房屋套内账户充值/消费记录详细信息
     */
    @ApiOperation("获取小区房屋套内账户充值/消费记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateRoomAccountRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateRoomAccountRecord estateRoomAccountRecord = estateRoomAccountRecordService.getById(id);
        return AjaxResult.success(estateRoomAccountRecord);
    }

    /**
     * 新增小区房屋套内账户充值/消费记录
     */
    @ApiOperation("新增小区房屋套内账户充值/消费记录")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:add')")
    @Log(title = "小区房屋套内账户充值/消费记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateRoomAccountRecord estateRoomAccountRecord) {
        return toAjax(estateRoomAccountRecordService.save(estateRoomAccountRecord) ? 1 : 0);
    }

    /**
     * 修改小区房屋套内账户充值/消费记录
     */
    @ApiOperation("修改小区房屋套内账户充值/消费记录")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:edit')")
    @Log(title = "小区房屋套内账户充值/消费记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateRoomAccountRecord estateRoomAccountRecord) {
        return toAjax(estateRoomAccountRecordService.updateById(estateRoomAccountRecord) ? 1 : 0);
    }

    /**
     * 删除小区房屋套内账户充值/消费记录
     */
    @ApiOperation("删除小区房屋套内账户充值/消费记录")
    @PreAuthorize("@ss.hasPermi('module:accountRecord:remove')")
    @Log(title = "小区房屋套内账户充值/消费记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateRoomAccountRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
